package com.chenpp.netty.tomcat.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.util.List;
import java.util.Map;

/**
 * 2020/3/9
 * created by chenpp
 */
public class CPRequest {

    private ChannelHandlerContext context ;
    private HttpRequest request ;

    public CPRequest(ChannelHandlerContext context, HttpRequest request){
        this.request  = request;
        this.context = context;
    }

    public String getMethod() {
        return request.method().name();
    }

    public String getUrl() {
        String url = request.uri();
        return url.substring(0,url.indexOf("?"));
    }

    public Map<String, List<String>> getParameters() {
        QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
        return decoder.parameters();
    }

    public String getParameter(String name) {
        Map<String, List<String>> params = getParameters();
        List<String> param = params.get(name);
        if (null == param) {
            return null;
        } else {
            return param.get(0);
        }
    }
}
